package com.example.jnnews;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;

public class AccountManagementActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private UserAdapter adapter;
    private List<User> userList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_management);

        loadUsers();
        setupRecyclerView();
    }

    private void loadUsers() {
        SharedPreferences prefs = getSharedPreferences("jnnews_prefs", MODE_PRIVATE);
        String json = prefs.getString("users", "");
        userList = new Gson().fromJson(json, new TypeToken<ArrayList<User>>(){}.getType());
        if (userList == null) userList = new ArrayList<>();
    }

    private void setupRecyclerView() {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UserAdapter(userList);
        recyclerView.setAdapter(adapter);
    }

    class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
        private List<User> users;

        UserAdapter(List<User> users) {
            this.users = users;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_user, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            User user = users.get(position);
            holder.tvUsername.setText(user.getUsername());

            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(AccountManagementActivity.this, ChangePasswordActivity.class);
                intent.putExtra("username", user.getUsername());
                startActivity(intent);
            });

            holder.itemView.setOnLongClickListener(v -> {
                new AlertDialog.Builder(AccountManagementActivity.this)
                        .setTitle("删除账号")
                        .setMessage("确定删除 " + user.getUsername() + " 吗？")
                        .setPositiveButton("删除", (dialog, which) -> {
                            users.remove(position);
                            saveUsers();
                            notifyItemRemoved(position);
                        })
                        .setNegativeButton("取消", null)
                        .show();
                return true;
            });
        }

        @Override
        public int getItemCount() {
            return users.size();
        }

        void saveUsers() {
            SharedPreferences prefs = getSharedPreferences("jnnews_prefs", MODE_PRIVATE);
            String json = new Gson().toJson(users);
            prefs.edit().putString("users", json).apply();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvUsername;

            ViewHolder(View itemView) {
                super(itemView);
                tvUsername = itemView.findViewById(R.id.tv_username);
            }
        }
    }
}